package com.test.controller;


import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.test.common.Page;
import com.test.common.R;
import com.test.common.SystemConstant;
import com.test.entity.SysUser;

import com.test.service.SysRoleService;
import com.test.service.SysUserService;

@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleService sysRoleService;

	/**
	 * 用户列表
	 * 
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list")
	public Page<SysUser> list(@RequestBody Map<String, Object> params) {
		if (getUserId() != SystemConstant.SUPER_ADMIN) {
			params.put("userIdCreate", getUserId());
		}
		return sysUserService.listUser(params);
	}

	/**
	 * 用户权限
	 */
	@ResponseBody
	@RequestMapping("/perms")
	public R listUserPerms() {
		return sysUserService.listUserPerms(getUserId());
	}

	/**
	 * 获取登录的用户信息
	 * 
	 */
	@ResponseBody
	@RequestMapping("/info")
	public R info() {
		return R.ok().put("user", getUser());
	}
	
	/**
	 * 新增用户，并关联角色
	 * @param user
	 * @return
	 */
	@RequestMapping("/save")
	public R saveUser(@RequestBody SysUser user) {
		return sysUserService.saveUser(user);
	}

	/**
	 * 删除用户信息，并移除对应关联角色关系
	 * @param userId
	 * @return
	 */
	@RequestMapping("/remove")
	public R removeUser(@RequestBody Long[] userId) {
		return sysUserService.batchRemove(userId);
	}
	
	/**
	 * 根据用户id获取用户及角色信息
	 * @param userId
	 * @return
	 */
	@RequestMapping("/infoUser")
	public R infoUser(@RequestBody Long userId) {
		return sysUserService.getUserById(userId);
	}
	
	/**
	 * 更新用户信息及角色信息
	 * @param user
	 * @return
	 */
	@RequestMapping("/update")
	public R updateUser(@RequestBody SysUser user) {
		return sysUserService.updateUser(user);
	}
	
	@RequestMapping("/resetPwd")
	public R resetPwd(@RequestBody SysUser user) {
		return sysUserService.updatePswd(user);
	}
}
